package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.MonitorFaultEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by ding on 2017/8/4.
 */
public interface MonitorFaultRepository extends CrudRepository<MonitorFaultEntity, Long> {

    /**
     * @Description: 故障审核DT
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:13
     */
    public List<DTMonitorFaultDto> getMonitorFaultCheck4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    public Integer getMonitorFaultCheckCount4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 获取单位审核人审核状况
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:13
     */
    @Query(value = "select lf.monitorCheck from MonitorFaultEntity lf where lf.monitorFaultId = ?1")
    public Integer getLampCheck(Integer lampFaultId) throws Exception;

    /**
     * @Description: 主管审核通过
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:18
     */
    @Modifying
    @Query(value = "UPDATE MonitorFaultEntity  mf SET mf.monitorMainCheck=?1, mf.monitorMainCheckerId=?4,mf.monitorFeedback=?2,mf.monitorMaintenanceStatus=1,mf.monitorMainCheckTime=?5 WHERE mf.monitorFaultId=?3")
    public Integer faultAudit2Success(Integer isPass, String feedback, Integer lampFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 主管审核拒绝
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:20
     */
    @Modifying
    @Query(value = "UPDATE MonitorFaultEntity  lf SET lf.monitorMainCheck=?1, lf.monitorMainCheckerId=?4,lf.monitorFeedback=?2,lf.monitorMainCheckTime=?5 WHERE lf.monitorFaultId=?3")
    public Integer faultAudit2Fail(Integer isPass, String feedback, Integer monitorFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 单位审核人审核
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:22
     */
    @Modifying
    @Query(value = "UPDATE MonitorFaultEntity  lf SET lf.monitorCheck= ?1,lf.monitorCheckerId=?4,lf.monitorFeedback=?2,lf.monitorCheckTime=?5 WHERE lf.monitorFaultId=?3")
    public Integer faultAudit(Integer isPass, String feedback, Integer monitorFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 故障处理DT
     * @Author: jiang-weiron
     * @Date: 2017/8/4 13:23
     */
    public List<DTMonitorFaultDto> getMonitorFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer lampMaintenanceStatus) throws Exception;

    public Integer getMonitorFaultHandleCount4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer lampMaintenanceStatus) throws Exception;

    /**
     * @Description: 添加施工方案
     * @Author: jiang-weirong
     * @Date: 2017/8/4 13:23
     */
    @Modifying
    @Query(value = "UPDATE MonitorFaultEntity  lf SET lf.monitorMaintenanceContent = ?2 WHERE lf.monitorFaultId =?1")
    public Integer addMonitorMaintenanceContent(Integer monitorFaultId, String monitorMaintenanceContent) throws Exception;

    /**
     * @Description: 完成施工
     * @Author: jiang-weirong
     * @Date: 2017/8/4 13:24
     */
    @Modifying
    @Query(value = "UPDATE MonitorFaultEntity  lf SET lf.monitorMaintenancePhoto = ?1,lf.monitorMaintainerId=?2,lf.monitorMaintenanceTime=?3,lf.monitorMaintenanceExplain=?4,lf.monitorMaintenanceStatus=2 WHERE lf.monitorFaultId =?5")
    public Integer finishMonitorMaintenance(String monitorMaintenancePhoto, Integer monitorMaintainerId, String monitorMaintenanceTime, String monitorMaintenanceExplain, Integer monitorFaultId) throws Exception;

    /**
     * @Description: 获取故障审核list4App
     * @Author: jiang-weirong
     * @Date: 2017/8/4 18:27
     */
    public List<MonitorFaultDto> getMonitorFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 信号灯详情
     * @Author: jiang-weirong
     * @Date: 2017/8/4 20:01
     */
    @Query(value = "select new com.hualude.roadsign.dto.MonitorFaultDto(sl.monitorDirection,sl.monitorRoadName,sl.monitorId,sl.monitorLongitude," +
            "sl.monitorLatitude,sl.monitorProvince,sl.monitorCity,sl.monitorArea,sl.monitorStartRoad," +
            "sl.monitorEndRoad,sl.monitorPhoto,sl.monitorPoleShape,sl.monitorPosition,sl.monitorInstallOrg," +
            "sl.monitorCreatorId,sl.monitorMainOrgId, sl.monitorOrgId) " +
            "from MonitorEntity sl where sl.monitorId=?1")
    public MonitorFaultDto getMonitorInfo2Fault(Integer monitorId) throws Exception;

    /**
     * @Description: 信号灯故障详情
     * @Author: jiang-weirong
     * @Date: 2017/8/4 20:29
     */
    @Query(value = "select new com.hualude.roadsign.dto.MonitorFaultDto(lf.monitorFaultLevel,lf.monitorFaultPhoto,lf.monitorFaultType,lf.monitorFaultReason," +
            "lf.monitorFaultCreatorId,lf.monitorFaultTime,lf.monitorCheck,lf.monitorMainCheck,lf.monitorFeedback, lf.monitorMoney) " +
            "from MonitorFaultEntity lf where lf.monitorFaultId=?1")
    public MonitorFaultDto getSignalLampFaultInfo2Fault(Integer monitorFaultId) throws Exception;


    /**
     * @Description: 监控箱故障datatable
     * @Author: pan-jianan
     * @Date: 2017/8/5 12:16
     */
    public Integer getMonitorReportFaultCount4DT(Integer actorId, Integer userId, Integer orgId, String strSearch) throws Exception;

    public List<DTMonitorFaultDto> getMonitorReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;

    /**
     * @Description: 更新故障
     * @Author: pan-jianan
     * @Date: 2017/8/5 12:16
     */
    @Modifying
    @Query(value = "UPDATE MonitorFaultEntity  mf SET mf.monitorFaultLevel = ?2,mf.monitorFaultType = ?3,mf.monitorFaultPhoto = ?4,mf.monitorFaultReason = ?5,mf.monitorMoney =?6 WHERE mf.monitorFaultId =?1")
    public Integer updateMonitorFault(Integer monitorFaultId, Integer monitorFaultLevel, Integer monitorFaultType, String monitorFaultPhoto, String monitorFaultReason, Double monitorMoney) throws Exception;

    /**
     * @Description: 删除故障
     * @Author: pan-jianan
     * @Date: 2017/8/5 12:16
     */
    @Modifying
    @Query(value = "delete from MonitorFaultEntity mf where mf.monitorFaultId=?1")
    public Integer deleteMonitorFaultByIds(Integer monitorFaultId) throws Exception;

    /**
     * @Description: 通过ID删除监控箱故障
     * @author xu_jiaHui
     * @date 2017/8/8 20:47
     */
    @Modifying
    @Query(value = "delete from MonitorFaultEntity  where monitorId=?1")
    public Integer deleteMonitorFault(int monitorId) throws Exception;

    /**
     * @Description: 是否可上报
     * @Author: pan-jianan
     * @Date: 2017/8/5 17:50
     */
    @Query(value = "select new com.hualude.roadsign.dto.MonitorFaultDto(mf.monitorCheck,mf.monitorMainCheck,mf.monitorMaintenanceStatus) " +
            "from MonitorFaultEntity mf where mf.monitorId=?1  ORDER BY mf.monitorFaultTime DESC ")
    public List<MonitorFaultDto> getMonitorFaultIsReport(Integer monitorId) throws Exception;


    /**
     * @Description: app获取监控箱故障维修列表
     * @Author: pan-jianan
     * @Date: 2017/8/5 19:27
     */
    public List<MonitorFaultDto> getMonitorFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception;

    /**
     * @Description: app 监控箱回执单
     * @Author: pan-jianan
     * @Date: 2017/8/5 19:27
     */
    @Query(value = "select new com.hualude.roadsign.dto.MonitorFaultDto(mf.monitorId,mf.monitorMainCheckTime, mf.monitorFaultLevel," +
            "mf.monitorFaultPhoto,mf.monitorFaultType,mf.monitorFaultReason,mf.monitorMaintenancePhoto,mf.monitorMaintenanceContent,mf.monitorMaintenanceExplain," +
            "mf.monitorMaintenanceTime, m.monitorLongitude, m.monitorLatitude, m.monitorProvince, m.monitorCity," +
            " m.monitorArea, m.monitorRoadName,m.monitorStartRoad,m.monitorEndRoad,mf.monitorFeedback)" +
            " from MonitorEntity m,MonitorFaultEntity mf " +
            "WHERE m.monitorId=mf.monitorId AND mf.monitorFaultId = ?1")
    public MonitorFaultDto getMonitorReceiptInfo4App(Integer monitorFaultId) throws Exception;

    /**
     * @Description: 故障list4App
     * @Author: jiang-weirong
     * @Date: 2017/8/9 17:36
     */
    public List<MonitorFaultDto> getMonitorFaultReportList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description:
     * @Author: jiang-weirong
     * @Date: 2017/9/11 11:33
     */
    @Query(value = "select new com.hualude.roadsign.dto.MonitorFaultDto( monitorId, monitorOrgId, monitorMainOrgId, monitorFaultType) from MonitorFaultEntity where monitorFaultId = ?1")
    public MonitorFaultDto getInfoByFaultId(Integer lampFaultId) throws Exception;

    @Modifying
    @Query(value = "UPDATE MonitorFaultEntity  lf SET lf.monitorCheck= ?1,lf.monitorCheckerId=?4,lf.monitorFeedback=?2,lf.monitorCheckTime=?5,lf.monitorMainCheck=?1,lf.monitorMainCheckTime=?5,lf.monitorMaintenanceStatus=1 WHERE lf.monitorFaultId=?3")
    public Integer straightFaultAudit(Integer isPass, String feedback, Integer lampFaultId, Integer userId, String dateNowStr);
}
